﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Utils;

namespace UI.ModEmployee
{
    public partial class EmpReInAdd : System.Web.UI.Page
    {
        EmployeeBiz employeeBiz = new EmployeeBiz();
        EmpReInBiz empReInBiz = new EmpReInBiz();       
        DefDicBiz defDicBiz = new DefDicBiz();
        string reInID = "0";
        string account = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
               // isSpec = user.IsSpecial;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
            if (Request.Params["ReInID"] != null)
            {
                reInID = Request.Params["ReInID"].ToString();
                btnAdd.Enabled = false;
            }
            else
            {
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
            }

            if (!IsPostBack)
            {
                if (reInID != "0")
                {
                    BindData(reInID);
                }
                else
                {
                    txtReInTime.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
                    defDicBiz.InitDropDownList("Admin_Work_State", ddlStateID, "2");

                    defDicBiz.InitDropDownList("Admin_Emp_Duty", ddlDutyID, "0");

                    new DepartmentBiz().InitDropDownList(ddlDeptID, "0");
                    defDicBiz.InitDropDownList("Admin_Work_Area", ddlAreaID, "0");

                }



            }
        }
        private void BindData(string reInID)
        {
            DataTable dt = empReInBiz.GetOneInfo(reInID);
            if (dt.Rows.Count > 0)
            {
                txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                txtOldDeptName.Text = dt.Rows[0]["OldDeptName"].ToString();
                txtOldDutyName.Text = dt.Rows[0]["OldDutyName"].ToString();
                txtOldAreaName.Text = dt.Rows[0]["OldAreaName"].ToString();
                txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();               
                txtTestTimeLong.Text = dt.Rows[0]["TestTimeLong"].ToString();
                defDicBiz.InitDropDownList(ddlReInTypeName, dt.Rows[0]["ReInTypeName"].ToString());
                defDicBiz.InitDropDownList("Admin_Work_State", ddlStateID, dt.Rows[0]["StateID"].ToString()); 
                defDicBiz.InitDropDownList("Admin_Emp_Duty", ddlDutyID, dt.Rows[0]["NewDutyName"].ToString());

                new DepartmentBiz().InitDropDownList(ddlDeptID, dt.Rows[0]["NewDeptName"].ToString());
                defDicBiz.InitDropDownList("Admin_Work_Area", ddlAreaID, dt.Rows[0]["NewAreaName"].ToString());

                string reInTime = dt.Rows[0]["ReInTime"].ToString();
                if (Utils.RegexRule.IsDate(reInTime))
                    txtReInTime.Text = Convert.ToDateTime(reInTime).ToString("yyyy-MM-dd");
                if (dt.Rows[0]["IsInsurance"].ToString() == "Y") cbInsurance.Checked = true;
                else
                    cbInsurance.Checked = false;
                txtMemo.Text = dt.Rows[0]["Memo"].ToString();

            }
            else
            {
                JSUtility.Alert(this, "无数据");
            }

        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmpReInAdd.aspx", RightChar.Add))
            {
                if (!AccountBiz.CheckRight(account, "EmpReInList.aspx", RightChar.Add))
                {
                    JSUtility.Alert(this, "您没有增加权限！");
                    return;
                }
            }
            string empCode = txtEmpCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(empCode))
            {
                JSUtility.Alert(this, "员工编号必须填写");
                return;
            }
            if (!employeeBiz.IsExistEmpCode(empCode,"N"))
            {
                JSUtility.Alert(this, "员工编号不存在（比如该员工离职了）");
                return;
            }
            string sataeID=ddlStateID.SelectedValue;
            if (sataeID == "0" ||sataeID=="3"|| ddlDutyID.SelectedValue == "0" || ddlDeptID.SelectedValue == "0" || ddlAreaID.SelectedValue == "0")
            {
                JSUtility.Alert(this, "请选择工作状态、部门、职务、工作区域");
                return;
            }
           
            string reInTime = txtReInTime.Text.Trim();
            if (!RegexRule.IsDate(reInTime))
            {
                JSUtility.Alert(this, "离职时间无效");
                return;
            }
            string testTimeLong = txtTestTimeLong.Text.Trim();
            if (!RegexRule.IsInt(testTimeLong))
            {
                JSUtility.Alert(this, "试用时长必须为整数");
                return;
            }
            if (sataeID == "1" && testTimeLong != "0")
            {
                JSUtility.Alert(this, "返聘为正式状态的，试用时长必须为0月");
                return;
            }
            if (sataeID != "1" && testTimeLong == "0")
            {
                JSUtility.Alert(this, "返聘为试用状态的，试用时长不能为0月");
                return;
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();

            sdList.Add("EmpCode", empCode);
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("StateID", ddlStateID.SelectedValue);
            sdList.Add("OldDutyName", txtOldDutyName.Text.Trim());
            sdList.Add("OldDeptName", txtOldDeptName.Text.Trim());
            sdList.Add("OldAreaName", txtOldAreaName.Text.Trim());
            sdList.Add("NewDutyName", ddlDutyID.SelectedItem.Text.Trim());
            sdList.Add("NewDeptName", StringUtility.RemoveLeftSpace(ddlDeptID.SelectedItem.Text.Trim()));
            sdList.Add("NewAreaName", ddlAreaID.SelectedItem.Text.Trim());
            sdList.Add("ReInTypeName", ddlReInTypeName.SelectedItem.Text.Trim());
            sdList.Add("ReInTime", reInTime);
            sdList.Add("TestTimeLong", testTimeLong);//

            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);
            sdList.Add("IsInsurance", (cbInsurance.Checked==true)?"Y":"N");

            //更改员工部门
            SortedDictionary<string, string> hList = new SortedDictionary<string, string>();
            hList.Add("EmpCode", txtEmpCode.Text.Trim());
            hList.Add("DutyID", ddlDutyID.SelectedValue);
            hList.Add("DeptID", ddlDeptID.SelectedValue);
            hList.Add("AreaID", ddlAreaID.SelectedValue);
            hList.Add("SynInsurance", XmlUtility.GetWebParam("SynInsurance"));
          

            string info = "";
            if (empReInBiz.Insert(sdList, hList))
            {
                info = "增加返聘复职信息成功";               
            }
            else
                info = "增加返聘复职信息失败";
            JSUtility.AlertAndRedirect(info, "EmpReInAdd.aspx");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmpReInAdd.aspx", RightChar.Mod))
            {
                if (!AccountBiz.CheckRight(account, "EmpReInList.aspx", RightChar.Mod))
                {
                    JSUtility.Alert(this, "您没有更新权限！");
                    return;
                }
            }
            string empCode = txtEmpCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(empCode))
            {
                JSUtility.Alert(this, "员工编号必须填写");
                return;
            }
            if (!employeeBiz.IsExistEmpCode(empCode))
            {
                JSUtility.Alert(this, "员工编号不存在");
                return;
            }
            string sataeID=ddlStateID.SelectedValue;
            if (sataeID == "0" ||sataeID=="3"|| ddlDutyID.SelectedValue == "0" || ddlDeptID.SelectedValue == "0" || ddlAreaID.SelectedValue == "0")
            {
                JSUtility.Alert(this, "请选择工作状态、部门、职务、工作区域");
                return;
            }
            string reInTime = txtReInTime.Text.Trim();
            if (!RegexRule.IsDate(reInTime))
            {
                JSUtility.Alert(this, "离职时间无效");
                return;
            }
            string testTimeLong = txtTestTimeLong.Text.Trim();
            if (!RegexRule.IsInt(testTimeLong))
            {
                JSUtility.Alert(this, "试用时长必须为整数");
                return;
            }
            if (sataeID == "1" && testTimeLong != "0")
            {
                JSUtility.Alert(this, "返聘为正式状态的，试用时长必须为0月");
                return;
            }
            if (sataeID != "1" && testTimeLong == "0")
            {
                JSUtility.Alert(this, "返聘为试用状态的，试用时长不能为0月");
                return;
            }

            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("ReInID", reInID);
            sdList.Add("EmpCode", empCode);
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("StateID", ddlStateID.SelectedValue);
            sdList.Add("OldDutyName", txtOldDutyName.Text.Trim());
            sdList.Add("OldDeptName", txtOldDeptName.Text.Trim());
            sdList.Add("OldAreaName", txtOldAreaName.Text.Trim());
            sdList.Add("NewDutyName", ddlDutyID.SelectedItem.Text.Trim());
            sdList.Add("NewDeptName", StringUtility.RemoveLeftSpace(ddlDeptID.SelectedItem.Text.Trim()));
            sdList.Add("NewAreaName", ddlAreaID.SelectedItem.Text.Trim());
            sdList.Add("ReInTypeName", ddlReInTypeName.SelectedItem.Text.Trim());
            sdList.Add("ReInTime", reInTime);
            sdList.Add("TestTimeLong", testTimeLong);
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);
            sdList.Add("IsInsurance", (cbInsurance.Checked == true) ? "Y" : "N");
            if (reInID != "0")
            {
                SortedDictionary<string, string> hList = new SortedDictionary<string, string>();
                hList.Add("EmpCode", txtEmpCode.Text.Trim());
                hList.Add("DutyID", ddlDutyID.SelectedValue);
                hList.Add("DeptID", ddlDeptID.SelectedValue);
                hList.Add("AreaID", ddlAreaID.SelectedValue);
                hList.Add("StateID", ddlStateID.SelectedValue);
                hList.Add("LeaveTime", "null");
                hList.Add("ReMark", System.DateTime.Now.ToString("yyyy-MM-dd") + "返聘");
                hList.Add("SynInsurance", XmlUtility.GetWebParam("SynInsurance"));
                string info = "";
                if (empReInBiz.Update(sdList, hList))
                {
                    info = "成功更新复职返聘信息";
                    BindData(reInID);
                }
                else
                    info = "更新返聘复职信息失败";
                JSUtility.Alert(this, info);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (AccountBiz.GetRange(account, "EmpReInList.aspx") != RangeChar.Super)
            {
                JSUtility.Alert(this, "您没有超级删除权限！");
                return;
            }
            if (!AccountBiz.CheckRight(account, "EmpReInAdd.aspx", RightChar.Del))
            {
                if (!AccountBiz.CheckRight(account, "EmpReInList.aspx", RightChar.Del))
                {
                    JSUtility.Alert(this, "您没有删除权限！");
                    return;
                }
            }
            if (reInID != "0")
            {
                if (empReInBiz.DeleteOneInfo(reInID))
                    JSUtility.Alert(this, "删除成功");
                else
                    JSUtility.Alert(this, "删除失败");
            }
            else
                JSUtility.Alert(this, "请先选择数据");
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            JSUtility.CloseWindow();
        }

     

        protected void txtEmpCode_TextChanged(object sender, EventArgs e)
        {
            string empCode = txtEmpCode.Text.Trim();
            if (!string.IsNullOrWhiteSpace(empCode))
            {
                if (Utils.StringUtility.IncludeHZ(empCode))//如果包含汉字
                {
                    DataTable dt = employeeBiz.GetOneInfoByName(empCode);
                    if (dt.Rows.Count == 0)
                    {
                        JSUtility.Alert(this, "不存在该员工姓名: " + empCode);
                    }
                    else if (dt.Rows.Count == 1)
                    {
                        if (dt.Rows[0]["StateName"].ToString() == "离职" || dt.Rows[0]["StateName"].ToString() == "请长假")
                        {
                            txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                            txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                            txtOldDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                            txtOldDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                            txtOldAreaName.Text = dt.Rows[0]["AreaName"].ToString();

                            defDicBiz.InitSynDropDownList(ddlDutyID, dt.Rows[0]["DutyName"].ToString());
                            defDicBiz.InitSynDropDownList(ddlDeptID, dt.Rows[0]["DeptName"].ToString());
                            defDicBiz.InitSynDropDownList(ddlAreaID, dt.Rows[0]["AreaName"].ToString());
                            defDicBiz.InitSynDropDownList(ddlStateID, dt.Rows[0]["StateID"].ToString());
                            if (dt.Rows[0]["StateName"].ToString() == "离职") ddlReInTypeName.SelectedIndex = 0;
                            else ddlReInTypeName.SelectedIndex = 1;
                        }
                        else
                            JSUtility.Alert(this, "该员工仍然在职: " + empCode);
                    }
                    else
                    {
                        string info = "存在同名，请输入工号：\n\n";
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            info += dt.Rows[i]["EmpCode"].ToString() + "  " + dt.Rows[i]["EmpName"].ToString() + "\n";
                        }
                        JSUtility.Alert(this, info);
                    }
                }
                else
                {
                    DataTable dt = employeeBiz.GetOneInfo(empCode);
                    if (dt.Rows.Count > 0)
                    {
                        if (dt.Rows[0]["StateName"].ToString() == "离职" || dt.Rows[0]["StateName"].ToString() == "请长假")
                        {
                            txtOldDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                            txtOldDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                            txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                            txtOldAreaName.Text = dt.Rows[0]["AreaName"].ToString();
                            defDicBiz.InitSynDropDownList(ddlDutyID, dt.Rows[0]["DutyName"].ToString());
                            defDicBiz.InitSynDropDownList(ddlDeptID, dt.Rows[0]["DeptName"].ToString());
                            defDicBiz.InitSynDropDownList(ddlAreaID, dt.Rows[0]["AreaName"].ToString());
                            defDicBiz.InitSynDropDownList(ddlStateID, dt.Rows[0]["StateID"].ToString());
                            if (dt.Rows[0]["StateName"].ToString() == "离职") ddlReInTypeName.SelectedIndex = 0;
                            else ddlReInTypeName.SelectedIndex = 1;
                        }
                        else
                            JSUtility.Alert(this, "该员工仍然在职: " + empCode);
                    }
                    else
                        JSUtility.Alert(this, "不存在该员工编号: " + empCode);
                }
            }
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
        }

       
    }
}